home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / usenet / volume4 / cookie / part01 next >
Encoding:
Internet Message Format  |  1988-05-20  |  9.1 KB

  1. Path: uunet!husc6!bloom-beacon!mit-eddie!uw-beaver!tektronix!tekgen!tekred!games
  2. From: games@tekred.TEK.COM
  3. Newsgroups: comp.sources.games
  4. Subject: v04i015:  cookie - another fortune cookie program, Part01/03
  5. Message-ID: <2543@tekred.TEK.COM>
  6. Date: 20 May 88 22:35:13 GMT
  7. Sender: billr@tekred.TEK.COM
  8. Lines: 329
  9. Approved: billr@saab.CNA.TEK.COM
  10.  
  11. Submitted by: uunet.uu.net!nuchat!sugar!karl (Karl Lehenbauer)
  12. Comp.sources.games: Volume 4, Issue 15
  13. Archive-name: cookie/Part01
  14.  
  15.     [Although not as extensive as fortune(6), this program is
  16.      smaller. It runs OK on my Sun 3/60.  -br]
  17.  
  18. #! /bin/sh
  19. # This is a shell archive.  Remove anything before this line, then unpack
  20. # it by saving it into a file and typing "sh file".  To overwrite existing
  21. # files, type "sh file -c".  You can also feed this as standard input via
  22. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  23. # will see the following message at the end:
  24. #        "End of archive 1 (of 3)."
  25. # Contents:  MANIFEST README Makefile cookie.h cookhash.c cookie.c
  26. # Wrapped by billr@saab on Fri May 20 15:23:02 1988
  27. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  28. if test -f MANIFEST -a "${1}" != "-c" ; then 
  29.   echo shar: Will not over-write existing file \"MANIFEST\"
  30. else
  31. echo shar: Extracting \"MANIFEST\" \(354 characters\)
  32. sed "s/^X//" >MANIFEST <<'END_OF_MANIFEST'
  33. X   File Name        Archive #    Description
  34. X-----------------------------------------------------------
  35. X MANIFEST                  1    This shipping list
  36. X Makefile                  1    
  37. X README                    1    
  38. X cookhash.c                1    
  39. X cookie.c                  1    
  40. X cookie.h                  1    
  41. X cookies.aa                2    
  42. X cookies.ab                3    
  43. END_OF_MANIFEST
  44. if test 354 -ne `wc -c <MANIFEST`; then
  45.     echo shar: \"MANIFEST\" unpacked with wrong size!
  46. fi
  47. # end of overwriting check
  48. fi
  49. if test -f README -a "${1}" != "-c" ; then 
  50.   echo shar: Will not over-write existing file \"README\"
  51. else
  52. echo shar: Extracting \"README\" \(1126 characters\)
  53. sed "s/^X//" >README <<'END_OF_README'
  54. XCookie programs and cookie file
  55. X-------------------------------
  56. X
  57. XVersion 0.0, By Karl Lehenbauer, 4/7/88
  58. X
  59. XYet another fortune cookie program...  The main thing that makes this 
  60. Xone interesting is the cookie file, which is abundantly full of quotes,
  61. Xcookies and such, of varying quality and accuracy.  In particular,
  62. Xit has many good quotes about software engineering, humanism, church
  63. Xand state, and the human condition.
  64. X
  65. XThe only machine that it for sure works on is a PC/AT clone running
  66. XMicroport System V/AT.  It should, however, work perfectly well on
  67. Xother System V systems.  To run under BSD, really_random() needs to
  68. Xbe twiddled.  Similarly small efforts should be required to get it 
  69. Xto run under V7, MS-DOS, etc.
  70. X
  71. XTo use, set the paths for the cookie and cookie hash files in cookie.h 
  72. Xthen move the cookie file, "cookies", to its final resting place.
  73. X
  74. XCompile and link cookie.c and cookhash.c   "make" should do it.
  75. X
  76. XGenerate the hash file to go along with the cookies, something like:
  77. Xcookhash <cookies >cookie.hash
  78. X
  79. X$ cookie
  80. X
  81. XDirect flames, kudos and questions to 
  82. X    ..!{uunet!nuchat}|{bellcore!tness1}!sugar!karl
  83. END_OF_README
  84. if test 1126 -ne `wc -c <README`; then
  85.     echo shar: \"README\" unpacked with wrong size!
  86. fi
  87. # end of overwriting check
  88. fi
  89. if test -f Makefile -a "${1}" != "-c" ; then 
  90.   echo shar: Will not over-write existing file \"Makefile\"
  91. else
  92. echo shar: Extracting \"Makefile\" \(251 characters\)
  93. sed "s/^X//" >Makefile <<'END_OF_Makefile'
  94. X# makefile for cookie program   4/8/88 remote from sugar
  95. X
  96. Xall:    cookie cookhash cookies
  97. X
  98. Xcookie:    cookie.c
  99. X    cc -O -o cookie cookie.c
  100. X
  101. Xcookhash:    cookhash.c
  102. X    cc -O -o cookhash cookhash.c
  103. X
  104. Xcookies:    cookies.aa cookies.ab
  105. X    cat cookies.aa cookies.ab >cookies
  106. END_OF_Makefile
  107. if test 251 -ne `wc -c <Makefile`; then
  108.     echo shar: \"Makefile\" unpacked with wrong size!
  109. fi
  110. # end of overwriting check
  111. fi
  112. if test -f cookie.h -a "${1}" != "-c" ; then 
  113.   echo shar: Will not over-write existing file \"cookie.h\"
  114. else
  115. echo shar: Extracting \"cookie.h\" \(170 characters\)
  116. sed "s/^X//" >cookie.h <<'END_OF_cookie.h'
  117. X/* cookie.h - include file for karl's cookie program */
  118. X
  119. X#define COOKIEFILE "/usr/local/lib/cookies"
  120. X#define HASHFILE "/usr/local/lib/cookie.hash"
  121. X
  122. X/* end of cookie.h */
  123. END_OF_cookie.h
  124. if test 170 -ne `wc -c <cookie.h`; then
  125.     echo shar: \"cookie.h\" unpacked with wrong size!
  126. fi
  127. # end of overwriting check
  128. fi
  129. if test -f cookhash.c -a "${1}" != "-c" ; then 
  130.   echo shar: Will not over-write existing file \"cookhash.c\"
  131. else
  132. echo shar: Extracting \"cookhash.c\" \(875 characters\)
  133. sed "s/^X//" >cookhash.c <<'END_OF_cookhash.c'
  134. X/* cookhash - read a sayings file and generate an index file */
  135. X/*  %M%  %I%  %H%  */
  136. X
  137. X#include <stdio.h>
  138. X
  139. X#define YES 1
  140. X#define NO 0
  141. X#define METACHAR '%'
  142. X
  143. Xmain(argc,argv)
  144. Xint argc;
  145. Xchar *argv[];
  146. X{
  147. X    int c, sawmeta;
  148. X    long charpos = 0;
  149. X
  150. X    if (argc != 1)
  151. X    {
  152. X        fprintf(stderr,"usage: cookhash <cookiefile >hashfile\n");
  153. X        exit(1);
  154. X    }
  155. X
  156. X    /* write out the "address" of the first cookie */
  157. X    puts("000000");
  158. X
  159. X    /* read the cookie until the end,
  160. X     *   whenever the end-of-cookie ("%%") sequence is found,
  161. X     *   the "address" (file position) of the first byte following
  162. X     *   it (start of next cookie) is written to the index (hash) file
  163. X     */
  164. X    while ((c = getchar()) != EOF)
  165. X    {
  166. X        if (c == METACHAR)
  167. X        {
  168. X            if (sawmeta)
  169. X            {
  170. X                printf("%06lx\n",charpos+2);
  171. X                sawmeta = NO;
  172. X            }
  173. X            else
  174. X                sawmeta = YES;
  175. X        }
  176. X        else
  177. X            sawmeta = NO;
  178. X        charpos++;
  179. X    }
  180. X    exit(0);
  181. X}
  182. X
  183. X/* end of cookhash.c */
  184. END_OF_cookhash.c
  185. if test 875 -ne `wc -c <cookhash.c`; then
  186.     echo shar: \"cookhash.c\" unpacked with wrong size!
  187. fi
  188. # end of overwriting check
  189. fi
  190. if test -f cookie.c -a "${1}" != "-c" ; then 
  191.   echo shar: Will not over-write existing file \"cookie.c\"
  192. else
  193. echo shar: Extracting \"cookie.c\" \(2654 characters\)
  194. sed "s/^X//" >cookie.c <<'END_OF_cookie.c'
  195. X/* cookie - print out an entry from the sayings file */
  196. X/*  %M%  %I%  %H%  */
  197. X
  198. X#include <stdio.h>
  199. X#include "cookie.h"
  200. X
  201. X#define ENTSIZE 7L
  202. X#define METACHAR '%'
  203. X#define YES 1
  204. X#define NO 0
  205. X
  206. Xchar *sccs_id = "@(#) fortune cookie program %I% %H% by K. Lehenbauer";
  207. X
  208. Xextern long lseek(), time();
  209. Xextern int rand();
  210. X
  211. Xchar *cookiename = COOKIEFILE;
  212. Xchar *hashname = HASHFILE;
  213. X
  214. X/* really_random - insure a good random return for a range, unlike an arbitrary
  215. X * random() % n, thanks to Ken Arnold, Unix Review, October 1987
  216. X * ...likely needs a little hacking to run under Berkely
  217. X */
  218. X#define RANDOM_RANGE ((1 << 15) - 1)
  219. Xint really_random(my_range)
  220. Xint my_range;
  221. X{
  222. X    int max_multiple, rnum;
  223. X
  224. X    max_multiple = RANDOM_RANGE / my_range;
  225. X    max_multiple *= my_range;
  226. X    while ((rnum = rand()) >= max_multiple)
  227. X        continue;
  228. X    return(rnum % my_range);
  229. X}
  230. X
  231. Xmain(argc,argv)
  232. Xint argc;
  233. Xchar *argv[];
  234. X{
  235. X    int nentries, oneiwant, c, sawmeta = 0;
  236. X    FILE *hashf, *cookief;
  237. X    long cookiepos;
  238. X
  239. X    /* if we got exactly three arguments, use the cookie and hash
  240. X     * files specified
  241. X     */
  242. X    if (argc == 3)
  243. X    {
  244. X        cookiename = argv[1];
  245. X        hashname = argv[2];
  246. X    }
  247. X    /* otherwise if argc isn't one (no arguments, specifying the
  248. X     * default cookie file), barf
  249. X     */
  250. X    else if (argc != 1)
  251. X    {
  252. X        fputs("usage: cookie cookiefile hashfile\n",stderr);
  253. X        exit(1);
  254. X    }
  255. X
  256. X    /* open the cookie file for read */
  257. X    if ((cookief = fopen(cookiename,"r")) == NULL)
  258. X    {
  259. X        perror(cookiename);
  260. X        exit(2);
  261. X    }
  262. X
  263. X    /* open the hash file for read */
  264. X    if ((hashf = fopen(hashname,"r")) == NULL)
  265. X    {
  266. X        perror(hashname);
  267. X        exit(2);
  268. X    }
  269. X
  270. X    /* compute number of cookie addresses in the hash file by
  271. X     * dividing the file length by the size of a cookie address
  272. X     */
  273. X    if (fseek(hashf,0L,2) != 0)
  274. X    {
  275. X        fputs("cookie: fseek failed\n",stderr);
  276. X        exit(3);
  277. X    }
  278. X    nentries = ftell(hashf) / 7L;
  279. X
  280. X    /* seed the random number generator with time in seconds plus
  281. X     * the program's process ID - it yields a pretty good seed
  282. X     * again, thanks to Ken Arnold
  283. X     */
  284. X    srand(getpid() + time(NULL));
  285. X
  286. X    /* generate a not really random number */
  287. X    oneiwant = really_random(nentries);
  288. X
  289. X    /* locate the one I want in the hash file and read the
  290. X     * address found there
  291. X     */
  292. X    fseek(hashf,(long)oneiwant * ENTSIZE, 0);
  293. X    fscanf(hashf,"%lx",&cookiepos);
  294. X
  295. X    /* seek cookie file to cookie starting at address read from hash */
  296. X    fseek(cookief,cookiepos,0);
  297. X
  298. X    /* get characters from the cookie file and write them out
  299. X     * until finding the end-of-fortune sequence, '%%'
  300. X     */
  301. X    while ((c = fgetc(cookief)) != EOF && sawmeta < 2)
  302. X    {
  303. X        if (c != METACHAR)
  304. X        {
  305. X            if (sawmeta)
  306. X                putchar(METACHAR);
  307. X            putchar(c);
  308. X            sawmeta = 0;
  309. X        }
  310. X        else
  311. X            sawmeta++;
  312. X    }
  313. X    exit(0);
  314. X}
  315. X
  316. X/* end of cookie.c */
  317. END_OF_cookie.c
  318. if test 2654 -ne `wc -c <cookie.c`; then
  319.     echo shar: \"cookie.c\" unpacked with wrong size!
  320. fi
  321. # end of overwriting check
  322. fi
  323. echo shar: End of archive 1 \(of 3\).
  324. cp /dev/null ark1isdone
  325. MISSING=""
  326. for I in 1 2 3 ; do
  327.     if test ! -f ark${I}isdone ; then
  328.     MISSING="${MISSING} ${I}"
  329.     fi
  330. done
  331. if test "${MISSING}" = "" ; then
  332.     echo You have unpacked all 3 archives.
  333.     rm -f ark[1-9]isdone
  334. else
  335.     echo You still need to unpack the following archives:
  336.     echo "        " ${MISSING}
  337. fi
  338. ##  End of shell archive.
  339. exit 0
  340.